﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Directory.Web
{
	public partial class ForgetPassword : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			ltMsg.Text = string.Empty;
			if (!IsPostBack)
			{
				MultiView1.ActiveViewIndex = 0;
			}
		}
		// 验证用户名
		protected void btnValidateUserName_Click(object sender, EventArgs e)
		{
			Directory.Model.UserInfo user = null;
			try
			{
				user = UserDataProxy.GetUserInfo(tbUserName.Text);
			}
			catch (ArgumentException)
			{
				ltMsg.Text = "用户名格式不正确。";
				return;
			}
			catch
			{
				ltMsg.Text = "服务器错误。";
				return;
			}
			if (user == null)
			{
				ltMsg.Text = "用户名不存在。";
				return;
			}

			this.MultiView1.ActiveViewIndex = 1;
			lbPasswordQuestion.Text = UserDataProxy.GetPasswordQuestion(user.UserName);
			ViewState.Add("UserName", user.UserName);
		}
		// 验证密码问题
		protected void btnValidatePasswordAnswer_Click(object sender, EventArgs e)
		{
			string username=ViewState["UserName"].ToString();
			ViewState.Remove("UserName");
			if (UserDataProxy.ValidatePasswordAnswer(username, lbPasswordQuestion.Text, tbPasswordAnswer.Text))
			{
				this.MultiView1.ActiveViewIndex = 2;
				try
				{
					string pwd= UserDataProxy.GeneratePassword(username);
					if (WebUtil.TryLogIn(username, pwd, false))
					{
						this.lbNewPassword.Text = pwd;
					}
					else
					{
						ltMsg.Text = "服务器错误。";
					}
				}
				catch
				{
					ltMsg.Text = "服务器错误。";
				}
			}
			else
			{
				ltMsg.Text = "输入答案错误。";
				return;
			}
		}

		// 返回上一步（第一步）
		protected void btnPreStep_Click(object sender, EventArgs e)
		{
			this.MultiView1.ActiveViewIndex = 0;
			ViewState.Remove("UserName");
		}
}
}
